package com.ex.volley;

import java.util.HashMap;
import java.util.Map;

import android.R.string;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.ex.util.AppUtils;
import com.example.androiddemo.App;

public class VolleyDemo {
	
	/**
	 * StringRequest， get
	 * @param url
	 */
	public static void stringReq(String url) {
		RequestQueue requestQueue = Volley.newRequestQueue(App.instance());
		StringRequest stringRequest = new StringRequest(url, new Listener<String>() {

			@Override
			public void onResponse(String response) {
				AppUtils.toast(response);
			}
		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				AppUtils.toast(error.getMessage());
			}
		});
//		stringRequest.setRetryPolicy(new DefaultRetryPolicy(6 * 10000, 
//				DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
//				1f));
		requestQueue.add(stringRequest);
		
	}
	
	/**
	 * StringRequest: post方法
	 * @param url
	 */
	public static void stringReqPost(String url) {
		RequestQueue requestQueue = Volley.newRequestQueue(App.instance());
		StringRequest stringRequest = new StringRequest(Method.POST, url, new Listener<String>() {

			@Override
			public void onResponse(String response) {
				AppUtils.toast(response);
			}
		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
//				AppUtils.toast(error.getMessage());
				AppUtils.volleyErrorHandle(error);
			}
		}) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				Map<String, String> map = new HashMap<String, String>();
				map.put("userName", "xiyi");
				map.put("age", "18");
				return map;
			}
		};
		requestQueue.add(stringRequest);
	}

}
